package shop.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import shop.DAO.UserDAO;
import shop.bean.UserBean;

public class UserServlet extends HttpServlet{


	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, java.io.IOException {
		
		if (request.getSession().getAttribute("currentUser") == null) {
			response.sendRedirect("login");
			return;
		}

		if(request.getParameter("name") != null 
				&& request.getParameter("password") != null
				&& request.getParameter("zipcode") != null
				&& request.getParameter("street_address") != null
				&& request.getParameter("city") != null
				&& request.getParameter("country") != null
				){
			try{
				UserBean o = (UserBean) request.getSession().getAttribute("currentUser");
				o.setName((String) request.getParameter("name"));
				o.setCity((String) request.getParameter("city"));
				o.setCountry(request.getParameter("country"));
				o.setStreetAddress(request.getParameter("street_address"));
				o.setZipcode((String) request.getParameter("zipcode"));
				if(((String) request.getParameter("password")) != ""){ 
					o.setPassword((String) request.getParameter("password"));
				}
				
				request.getSession().setAttribute("currentUser", o);
				UserDAO.saveUser(o);
			}
			catch (Throwable theException){System.out.println(theException);}
		}

		doGet(request, response);
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, java.io.IOException {
		if (request.getSession().getAttribute("currentUser") == null) {
			response.sendRedirect("login");
			return;
		}

		request.getRequestDispatcher("profile.jsp").forward(request,response);	

	}
}
